source /usr/lib/ns-allinone-2.1b4/ns-2/tcl/mcast/srm-nam.tcl ;# to separate $
source /usr/lib/ns-allinone-2.1b4/ns-2/tcl/mcast/srm-debug.tcl ;# to trace del$
Simulator set NumberInterfaces_ 1
set ns [new Simulator]
Simulator set EnableMcast_ 1
$ns trace-all [open out.tr w]
$ns namtrace-all [open out.nam w]
set srmSimType Probabilistic
$ns color 0 red ;#data source
$ns color 40 blue ;#session
$ns color 41 green ;#request
$ns color 42 white ;#repair
$ns color 4 red ;#source node

# Creating The Nodes
set nmax 8
for {set i 0} {$i <= $nmax} {incr i} {
      set n($i) [$ns node]
}
$n(1) color "red"

# Creating The Links
for {set i 1} {$i <= $nmax} {incr i} {
      $ns duplex-link $n($i) $n(0) 1.5Mb 10ms DropTail
}

# Orienting The Links
$ns duplex-link-op $n(0) $n(1) orient right
$ns duplex-link-op $n(0) $n(2) orient right-up
$ns duplex-link-op $n(0) $n(3) orient up
$ns duplex-link-op $n(0) $n(4) orient left-up
$ns duplex-link-op $n(0) $n(5) orient left
$ns duplex-link-op $n(0) $n(6) orient left-down
$ns duplex-link-op $n(0) $n(7) orient down
$ns duplex-link-op $n(0) $n(8) orient right-down
set group 0x8000
set cmc [$ns mrtproto CtrMcast {}]
$ns at 0.3 "$cmc switch-treetype $group"

# SRM TRACE EVENTS
set srmStats [open srmStats.tr w]
set srmEvents [open srmEvents.tr w]
set fid 0
for {set i 1} {$i <= $nmax} {incr i} {
      set srm($i) [new Agent/SRM/$srmSimType]
      $srm($i) set dst_ $group
      $srm($i) set fid_ [incr fid]
      $srm($i) log $srmStats
      $srm($i) trace $srmEvents
      $ns at 0.5 "$srm($i) start"
      $ns attach-agent $n($i) $srm($i)
}

# Attach a CBR Agent to srm(1)
set packetSize 800
set s [new Application/Traffic/CBR]
$s set packet_size_ $packetSize
$s set interval_ 0.02
$s attach-agent $srm(1)
$srm(1) set tg_ $s
$srm(1) set app_fid_ 0
$srm(1) set packetSize_ $packetSize
$ns at 2.0 "$srm(1) start-source"
set loss_module [new SRMErrorModel]
$loss_module drop-packet 2 10 1
$loss_module drop-target [$ns set nullAgent_]
$ns at 0.75 "$ns lossmodel $loss_module $n(1) $n(0)"
$ns at 4.0 "finish $s" proc distDump interval {
      global ns srm
      foreach i [array names srm] {
            set dist [$srm($i) distances?]
            if {$dist != ""} {
                  puts "[format %7.4f [$ns now]] distances $dist"
            }
      }
      $ns at [expr [$ns now] + $interval] "distDump $interval"
}
proc finish src {
      global prog ns env srmStats srmEvents srm nmax
      $src stop
      $ns flush-trace
      close $srmStats
      close $srmEvents
      puts "converting output to nam format..."
      if [info exists env(DISPLAY)] {
            puts "running nam..."
            exec nam out.nam &
      } else {
            exec cat srmStats.tr >@stdout
      }
      exit 0 
}
$ns run


